package com.ruoyi.quartz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.alarmmanagement.domain.DmWarn;
import com.ruoyi.basedata.domain.DmWarnStrategy;
import com.ruoyi.basedata.mapper.DmWarnStrategyMapper;
import com.ruoyi.common.utils.DateUtil;
import com.ruoyi.quartz.domain.UltralimitRoom;
import com.ruoyi.quartz.mapper.RunXbWarnMapper;
import com.ruoyi.quartz.service.IRunXbWarnsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RunXbWarnsServiceImpl implements IRunXbWarnsService {

    @Autowired
    private DmWarnStrategyMapper dmWarnStrategyMapper;
    @Autowired
    private RunXbWarnMapper runXbWarnMapper;

    @Override
    public void job_Ultralimit() {
         String beginTime = DateUtil.past7Days();
         String balanceDate = DateUtil.getLastDayDate();
         DmWarnStrategy dmWarnStrategy = dmWarnStrategyMapper.selectOne(new LambdaQueryWrapper<DmWarnStrategy>()
                 .eq(DmWarnStrategy::getStrategyName,"小表日用量超上下限预警"));
         //获取超上限的数据
         List<UltralimitRoom> ultralimitRooms1 = runXbWarnMapper.selectUltralimit(beginTime, balanceDate, dmWarnStrategy.getLevel1Up(), null);
        List<DmWarn> alarms = new ArrayList<DmWarn>();
         ultralimitRooms1.forEach(ultralimitRoom -> {
             DmWarn dmWarn = new DmWarn();
             dmWarn.setDescription("昨日用量超上限，当日用量"+ultralimitRoom.getCumulativeFlow()+"过去七日平均用量"+ultralimitRoom.getAvgFlow());
             dmWarn.setWarnStatus(0);
             dmWarn.setWarnType(1);
             dmWarn.setHandleStatus(0);
             dmWarn.setTargetType(1);
             dmWarn.setFreezeTime(ultralimitRoom.getFreezeTime());
             dmWarn.setStartTime(new Date());
             alarms.add(dmWarn);
         });

        //获取低于下限的数据
         List<UltralimitRoom> ultralimitRooms2 = runXbWarnMapper.selectUltralimit(beginTime, balanceDate, null, dmWarnStrategy.getLevel1Down());
         ultralimitRooms2.forEach(ultralimitRoom -> {
             DmWarn dmWarn = new DmWarn();
             dmWarn.setDescription("昨日用量低于下限，当日用量"+ultralimitRoom.getCumulativeFlow()+"过去七日平均用量"+ultralimitRoom.getAvgFlow());

             dmWarn.setWarnStatus(0);
             dmWarn.setWarnType(2);
             dmWarn.setHandleStatus(0);
             dmWarn.setTargetType(1);
             dmWarn.setFreezeTime(ultralimitRoom.getFreezeTime());
             dmWarn.setStartTime(new Date());
             alarms.add(dmWarn);
         });
        if(alarms.size()>0) {
            batchAlarmsWithSize(alarms, 100);
        }
    }
    private void batchAlarmsWithSize(List<DmWarn> alarms, int size) {
        int totalSize = alarms.size();//list长度
        int times = totalSize/size;
        int num = totalSize%size;
        times = num>0 ? times+1 : times;
        for(int i=0;i<times;i++) {
            if(i<times-1) {
                runXbWarnMapper.insertAlarmBatch(alarms.subList(100*i, 100*(i+1)));//插入预警记录到预警表
            }else if(num==0 && i==times-1){
                runXbWarnMapper.insertAlarmBatch(alarms.subList(100*i, 100*(i+1)));//插入预警记录到预警表
            }else if(num!=0 && i==times-1) {
                runXbWarnMapper.insertAlarmBatch(alarms.subList(100*i,100*i + num));//插入预警记录到预警表
            }
        }
    }
}
